import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class GraphDemo {
    //vertexList全部顶点
    private ArrayList<String> vertexList;
    //图的边,邻接矩阵
    private int[][] edge;
    //边的数目
    private int numOfEdge;
    //结点是否被访问
    private boolean[] isVisit;

    public static void main(String[] args) {
        int n = 5;  //结点的个数
        String Vertexs[] = {"A", "B", "C", "D", "E"};

        GraphDemo graphDemo = new GraphDemo(n);
        for(String vertex : Vertexs){
            graphDemo.insertVertex(vertex);
        }

        //添加边
        //A-B A-C B-C B-D B-E
        graphDemo.insertEdge(0,1,1);
        graphDemo.insertEdge(0,2,1);
        graphDemo.insertEdge(1,2,1);
        graphDemo.insertEdge(1,3,1);
        graphDemo.insertEdge(1,4,1);

        //显示邻接矩阵
        graphDemo.showGraph();
        //graphDemo.dfs();
        graphDemo.bfs();
    }

    //构造器
    public GraphDemo(int n) {
        vertexList = new ArrayList<String>(n);
        edge = new int[n][n];
        numOfEdge = 0;
    }

    //插入节点
    public void insertVertex(String vertex){
        vertexList.add(vertex);
    }

    //插入边
    public void insertEdge(int v1, int v2, int weight){
        edge[v1][v2] = weight;
        edge[v2][v1] = weight;
        numOfEdge++;
        isVisit = new boolean[5];
    }

    //返回节点个数
    public int getNumOfEdge(){
        return numOfEdge;
    }

    //返回结点的个数
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //显示图对应的矩阵
    public void showGraph(){
        for(int[] link : edge){
            System.out.println(Arrays.toString(link));
        }
    }

    //返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C"
    public String getValueByIndex(int i){
        return vertexList.get(i);
    }

    //返回v1和v2的权值
    public int getWeight(int v1, int v2){
        return edge[v1][v2];
    }

    //第一个邻接结点下标w
    public int getFirstNeighbor(int index){
        for(int j = 0; j < vertexList.size(); j++){
            if(edge[index][j] > 0){
                return j;
            }
        }
        return -1;
    }

    //下一个邻接节点
    public int getNextNeightbor(int v1, int v2){
        for(int j = v2 + 1; j < vertexList.size(); j++){
            if(edge[v1][j] > 0){
                return j;
            }
        }
        return -1;
    }

    //dfs算法
    public void dfs(boolean[] isVisited, int i){
        System.out.println(getValueByIndex(i) + "->");
        isVisited[i] = true;
        int w = getFirstNeighbor(i);
        while(w != -1){
            if(!isVisited[w]){
                dfs(isVisited,w);
            }
            w = getNextNeightbor(i,w);
        }
    }

    //遍历dfs
    public void dfs(){
        for(int i = 0; i < getNumOfVertex(); i++){
            if(!isVisit[i]){
                dfs(isVisit,i);
            }
        }
    }

    //bfs算法
    public void bfs(boolean[] isVisited, int i){
        //队列头结点下标
        int u;
        //邻接节点下标
        int w;
        LinkedList queue = new LinkedList();
        System.out.println(getValueByIndex(i) + "->");
        isVisited[i] = true;
        queue.addLast(i);

        while(!queue.isEmpty()){
            u = (Integer)queue.removeFirst();
            w = getFirstNeighbor(u);
            while(w != -1){
                if(!isVisited[w]){
                    System.out.println(getValueByIndex(w) + "->");
                    isVisited[w] = true;
                    queue.addLast(w);
                }
                w = getNextNeightbor(u,w);
            }

        }
    }

    //遍历bfs
    public void bfs(){
        for(int i = 0; i < getNumOfVertex(); i++){
            if(!isVisit[i]){
                bfs(isVisit,i);
            }
        }
    }

}
